;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Report.PurchaseRequestDetailReport", [])
        .controller("PurchaseRequestDetailReportCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", "UserContextService", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout, UserContextService) {
            $scope.markInfo = {
                allChecked: false,
                curRow: -1
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.toolbarConfig = {
                showColConfig: false
            };



            $scope.condition = {};
            $scope.dataList = [];
            $scope.tbConf = {
                scrollWidth: 1600,
                tableCol: [
                    {
                        type: "",
                        name: "采购计划单号",
                        dataField: "buyPlanOrder",
                        width: 180,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "采购零件编号",
                        dataField: "materialsNo",
                        width: 180,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "采购零件中文名",
                        dataField: "materialsDes",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "采购计划员",
                        dataField: "buyPlanCreateName",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "计划下达时间",
                        dataField: "buyPlanCreateDate",
                        width: 140,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "采购订单号",
                        dataField: "billNo",
                        width: 200,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "采购员",
                        dataField: "purchaseRequestCreateName",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "采购下单时间",
                        dataField: "purchaseRequestCreateDate",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "采购入库时间",
                        dataField: "allInboundTime",
                        width: 140,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },

                    {
                        type: "",
                        name: "SAP计划下达时间",
                        dataField: "sapPlanIssuedTimeShow",
                        width: 140,
                        isShow: $scope.isSCH,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "采购组",
                        dataField: "purchaseGroup",
                        width: 140,
                        isShow: $scope.isSCH,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "采购凭证",
                        dataField: "voucherNo",
                        width: 140,
                        isShow: $scope.isSCH,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        name: "交货日期",
                        dataField: "deliveryDateShow",
                        width: 140,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "是否按期交货",
                        dataField: "deliveryTimely",
                        width: 140,
                        isShow: true,
                        isFixed: false,
                    },
                ]
            };

            $scope.exportParam = {
                // sectionUrl: ybHttp.RequestUrl("device", "/SappostErrorRecord/export"),
                // sectionParam: {ids: []},
                allUrl: ybHttp.RequestUrl("device", "/purchaseRequest/orderMaterials/report/export",$scope.condition)
            };



            $scope.checkDate = function (obj, startFiled, endFiled) {
                var timeFormatObj = ybWidget.CheckDate(obj[startFiled], obj[endFiled]);
                if (!!timeFormatObj.tStart) {
                    obj[startFiled] = timeFormatObj.tStart;
                }
                if (!!timeFormatObj.tEnd) {
                    obj[endFiled] = timeFormatObj.tEnd;
                }
            };

            function getDataList(curPage) {
                $scope.markInfo.allChecked = false;
                $scope.markInfo.curRow = -1;
                // $scope.exportParam.sectionParam.ids = [];
                var service = "device";
                var reqUrl = "/purchaseRequest/orderMaterials/report";
                var param = {};
                param.pageSize = $scope.pageInfo.pageSize;
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                param.pageNum = $scope.pageInfo.curPage;
                param.filters = angular.copy($scope.condition);
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.list, function (item) {
                        item.buyPlanCreateDate = $filter("YbDate")(item.buyPlanCreateDate,"yyyy-MM-dd HH:mm:ss");
                        item.purchaseRequestCreateDate = $filter("YbDate")(item.purchaseRequestCreateDate,"yyyy-MM-dd HH:mm:ss");
                        item.allInboundTime = $filter("YbDate")(item.allInboundTime,"yyyy-MM-dd HH:mm:ss");
                        item.deliveryDateShow = $filter("YbDate")(item.deliveryDate,"yyyy-MM-dd HH:mm:ss");
                        item.sapPlanIssuedTimeShow = $filter("YbDate")(item.sapPlanIssuedTime,"yyyy-MM-dd HH:mm:ss");
                    });
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                });
            }

            $scope.toSort = function (config, index) {
                ybWidget.ToSort(config, index);
            };

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.selectRow = function (index) {
                $scope.markInfo.cueRow = index
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            $scope.reset = function () {
                $scope.condition = {};
            };

            function getBillIds() {
                // $scope.exportParam.sectionParam.ids = [];
                // angular.forEach($scope.dataList, function (item) {
                //     if (!!item.isChecked) {
                //         $scope.exportParam.sectionParam.ids.push(item.id);
                //     }
                // });
            }

            $scope.selectAll = function (list, type) {
                for (var i = 0, len = list.length; i < len; i++) {
                    list[i].isChecked = angular.isUndefined(type) ? $scope.markInfo.allChecked : $scope.markInfo["allChecked" + type];
                }
                getBillIds();
            };

            function checkAll(list) {
                var selectAll = false;
                for (var i = 0, len = list.length; i < len; i++) {
                    if (!list[i].isChecked) {
                        selectAll = false;
                        break;
                    }
                    selectAll = true;
                }
                return selectAll;
            }

            // $scope.selectSingle = function (item, list, type) {
            //     var selectAll = false;
            //     if (!item.isChecked) {
            //         selectAll = false;
            //     } else {
            //         if (!checkAll(list)) {
            //             selectAll = false;
            //         } else {
            //             selectAll = true;
            //         }
            //     }
            //     if (angular.isDefined(type)) {
            //         $scope.markInfo["allChecked" + type] = selectAll;
            //     } else {
            //         $scope.markInfo.allChecked = selectAll;
            //     }
            //     getBillIds();
            // };

            (function () {
                getDataList();
                // UserContextService.SetBtnPermission(setBtnPermission);
            })();
        }])
    ;
})(window, window.angular);
